.TH std::alignment_of 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::alignment_of \- std::alignment_of

.SH Synopsis
   Defined in header <type_traits>
   template< class T >              \fI(since C++11)\fP
   struct alignment_of;

   Provides the member constant value equal to the alignment requirement of the type T,
   as if obtained by an alignof expression. If T is an array type, returns the
   alignment requirements of the element type. If T is a reference type, returns the
   alignment requirements of the type referred to.

   If alignof(T) is not a valid expression, the behavior is undefined.

   If the program adds specializations for std::alignment_of
   or std::alignment_of_v
   \fI(since C++17)\fP, the behavior is undefined.

.SH Member constants

   value    alignof(T)
   \fB[static]\fP \fI(public static member constant)\fP

.SH Member functions

   operator std::size_t converts the object to std::size_t, returns value
                        \fI(public member function)\fP
   operator()           returns value
   \fI(C++14)\fP              \fI(public member function)\fP

.SH Member types

   Type       Definition
   value_type std::size_t
   type       std::integral_constant<std::size_t, value>

.SH Possible implementation

   template<class T>
   struct alignment_of : std::integral_constant<std::size_t, alignof(T)> {};

.SH Notes

   This type trait predates the alignof keyword, which can be used to obtain the same
   value with less verbosity.

.SH Example


// Run this code

 #include <cstdint>
 #include <iostream>
 #include <type_traits>

 struct A {};
 struct B
 {
     std::int8_t p;
     std::int16_t q;
 };

 int main()
 {
     std::cout << std::alignment_of<A>::value << ' ';
     std::cout << std::alignment_of<B>::value << ' ';
     std::cout << std::alignment_of<int>() << ' '; // alt syntax
     std::cout << std::alignment_of_v<double> << '\\n'; // c++17 alt syntax
 }

.SH Possible output:

 1 2 4 8

.SH See also

   alignof operator\fI(C++11)\fP      queries alignment requirements of a type
   alignas specifier\fI(C++11)\fP     specifies that the storage for the variable should be
                                aligned by specific amount
   aligned_storage              defines the type suitable for use as uninitialized
   \fI(C++11)\fP(deprecated in C++23) storage for types of given size
                                \fI(class template)\fP
   aligned_union                defines the type suitable for use as uninitialized
   \fI(C++11)\fP(deprecated in C++23) storage for all given types
                                \fI(class template)\fP
   max_align_t                  trivial type with alignment requirement as great as any
   \fI(C++11)\fP                      other scalar type
                                \fI(typedef)\fP
